package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.ErpBidataMapper;
import com.monitor.dao.ErpCustomerCarMapper;
import com.monitor.dao.ErpCustomerMapper;
import com.monitor.entity.ErpBidata;
import com.monitor.entity.ErpCustomer;
import com.monitor.entity.ErpCustomerCar;
import com.monitor.service.ErpCustomerCarService;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import com.monitor.util.enums.PositonUtil;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户车辆表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-05
 */
@Service
public class ErpCustomerCarServiceImpl extends ServiceImpl<ErpCustomerCarMapper, ErpCustomerCar> implements ErpCustomerCarService {

    @Resource
    private ErpCustomerCarMapper carMapper;
    
    @Resource
    private ErpCustomerMapper erpCustomerMapper;
    
    @Resource
    private ErpBidataMapper erpBidataMapper;

    @Override
    public List<Map<String, Object>> getCatInsurance(String custIds,Integer page,Integer limit) {
        return carMapper.getCatInsurance(custIds);
    }

    @Override
    public List<Map<String, Object>> getCatInsuranceList(String custIds) {
        return carMapper.getCatInsuranceList(custIds);
    }

	@Override
	public Result<ErpCustomerCar> addAndUpdate(ErpCustomerCar customerCar, int userId) {
		// TODO Auto-generated method stub
		Result<ErpCustomerCar> result=new Result<ErpCustomerCar>();
        
        try{
        	if(null!=customerCar.getId()&&customerCar.getCarPlate()!=null) {
    	    	ErpBidata bi=erpBidataMapper.selectByPlate(customerCar.getCarPlate());
    	    	if(bi!=null) {
    	    		bi.setCarPlate(customerCar.getCarPlate());
    	    		erpBidataMapper.updateAllColumnById(bi);
    	    	}
    	    	carMapper.updateById(customerCar);
        	}else {
        		int count=carMapper.selectCount(new EntityWrapper<ErpCustomerCar>().eq("car_plate", customerCar.getCarPlate()));
                if(count>0) {
                	return new Result<>(2, "车牌号重复");
                }
        		ErpCustomer cust=erpCustomerMapper.selectById(customerCar.getCustomerId());
            	if(customerCar.getCarType1()!=null&&customerCar.getCarType1().equals("货运车辆")) {
            		int num=cust.getCusTruck()+1;
            		cust.setCusTruck(num);
            	}
            	if(customerCar.getCarType1()!=null&&customerCar.getCarType1().equals("客运车辆")) {
            		int num=cust.getCusTruck()+1;
            		cust.setCusTruck(num);
            	}
            	if(customerCar.getCarType2()!=null&&customerCar.getCarType2().equals("挂车")) {
            		int num=cust.getCusGCar()+1;
            		cust.setCusGCar(num);
            	}
            	if(customerCar.getCarType2()!=null&&customerCar.getCarType2().equals("拖头")) {
            		int num=cust.getCusTCar()+1;
            		cust.setCusTCar(num);
            	}
            	if(customerCar.getCarType2()!=null&&customerCar.getCarType2().equals("单机")) {
            		int num=cust.getCusDCar()+1;
            		cust.setCusDCar(num);
            	}
            	if(customerCar.getCarType2()!=null&&customerCar.getCarType2().equals("小型车")) {
            		int num=cust.getCusXCar()+1;
            		cust.setCusXCar(num);
            	}
        		customerCar.setCarOperateDate(DataUtil.getStringDateShort());
    	    	int carType=PositonUtil.carWheelToCarType(customerCar.getCarWheel());
    	    	customerCar.setCarType3(carType);
    	    	int total=cust.getCusVehicletotal()+1;
    	    	cust.setCusVehicletotal(total);
    	    	erpCustomerMapper.updateAllColumnById(cust);
    	    	ErpBidata data=new ErpBidata();
    	    	data.setAddDate(DataUtil.getStringDateShort());
    	    	data.setCarNum(customerCar.getCarNumber());
    	    	data.setCarPlate(customerCar.getCarPlate());
    	    	data.setCarType(customerCar.getCarType1());
    	    	data.setCusId(customerCar.getCustomerId());
    	    	data.setTirePressure(customerCar.getCarWheel());
    	    	data.setUserId(userId);
    	    	data.setUserName(customerCar.getInputAttn());
    	    	erpBidataMapper.insert(data);
    	    	carMapper.insert(customerCar);
        	}
        	
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){
        	e.printStackTrace();
        	return new Result<>(1, e.getMessage());
        }
        return result;
	}

	@Override
	public ErpCustomerCar selectByCarPlate(String carPlate) {
		// TODO Auto-generated method stub
		ErpCustomerCar car=carMapper.selectByPlate(carPlate);
		return car;
	}
}
